package net
{
	import flash.utils.ByteArray;

	public class PackageIn extends ByteArray
	{
		public static const HEADER_SIZE:Number = 18;
		
		private var _len:int;
		
		private var _checksum:int;
		
		private var _clientId:int;
		
		private var _code:int;
		
		private var _extend1:int;
		
		private var _extend2:int;
		
		public function load(src:ByteArray,len:int):void
		{
			for(var i:int=0;i<len;i++)
			{
				writeByte(src.readByte());
			}
			this.position = 0;
			readHeader();
		}
		
		public function loadE(src:ByteArray,len:int,key:ByteArray):void
		{
			var i:int=0;
			var origion:ByteArray = new ByteArray();
			var result:ByteArray = new ByteArray();
			for(i=0;i<len;i++)
			{
				origion.writeByte(src.readByte());
				result.writeByte(origion[i]);
			}
			for(i=0;i<len;i++)
			{
				if(i>0)
				{
					key[i % 8] = (key[i % 8] + origion[i-1]) ^ i;
					result[i] = (origion[i] - origion[i-1]) ^ key[i % 8];
				}else
				{
					result[i] = origion[i] ^ key[0];
				}
			}
			result.position = 0;
			for(i = 0; i < len; i ++)
			{
//				writeByte(src.readByte() ^((key++ & (0xff << 16)) >> 16));
				writeByte(result.readByte());
			}
			position = 0;
			readHeader();
		}
		
		public function readHeader():void
		{
			readShort();
			_len = readInt();
			_code = readInt();
			_extend1 = readInt();
			_extend2 = readInt();
		}
		
		public function get checkSum():int
		{
			return _checksum;
		}
		
		public function get code():int
		{
			return _code;
		}
		
		public function get clientId():int
		{
			return _clientId;
		}
		
		public function get extend1():int
		{
			return _extend1;
		}
		
		public function get extend2():int
		{
			return _extend2;
		}
		
		
		public function get Len():int
		{
			return _len;
		}
		
		public function calculateCheckSum():int
		{
            var val1:int = 0x77;
            var i:int = 6;
			
            while (i < length)
            {
                val1 += this[i++];
            }

            return (val1) & 0x7F7F;
		}
		
		public function readXml():XML
		{
			return new XML(readUTF());
		}
		
		public function readDateString():String
		{
			return readShort() + "-" + readByte() + "-" + readByte() + " " + readByte() + ":" + readByte() + ":" + readByte();
		}
		
		public function readDate():Date
		{
			var year:int = readShort();
			var month:int = readByte() - 1;
			var day:int = readByte();
			var hours:int = readByte();
			var minutes:int = readByte();
			var seconds:int = readByte();
			var date:Date = new Date(year,month,day,hours,minutes,seconds);
			return date;
		}
		
		public function readByteArray():ByteArray
		{
			var temp:ByteArray = new ByteArray();
			readBytes(temp,0,_len - position);
			temp.position = 0;
			return temp;
		}
		
		public function deCompress():void
		{
			position = HEADER_SIZE;
			var temp:ByteArray = new ByteArray();
			readBytes(temp,0,_len - HEADER_SIZE);
			temp.uncompress();
			position = HEADER_SIZE;
			writeBytes(temp,0,temp.length);
			_len = HEADER_SIZE + temp.length;
			position = HEADER_SIZE;
		}
	}
}